using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Text;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using Org.Eurekaa.DNJ.Helpers;
using System.Web.UI.HtmlControls;
using Org.Eurekaa.DNJ.UI.DNJSettings;
using System.Reflection;
using System.IO;
using Org.Eurekaa.DNJ.Settings;


namespace Org.Eurekaa.DNJ.UI.WebControls
{
    [ToolboxBitmap(typeof(Org.Eurekaa.DNJ.UI.Resources.icons.DNJUIIcons), "DNJDatePicker.bmp")]
    [Designer(typeof(Org.Eurekaa.DNJ.UI.Design.WebControls.JQueryUIControlDesigner))]
    public class DNJDatePicker : JQueryUIControl
    {
        private HtmlGenericControl wrapper = new HtmlGenericControl("span");
        private HtmlGenericControl div = new HtmlGenericControl("div");
        private TextBox textbox = new TextBox();




        private DNJDatePickerSettings _settings = new DNJDatePickerSettings();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Settings")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJDatePickerSettings Settings
        {
            get { return _settings; }
            set { _settings = value; }
        }

        private DNJDatePickerEvents _events = new DNJDatePickerEvents();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Events")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJDatePickerEvents ClientEvents
        {
            get { return _events; }
            set { _events = value; }
        }

        [Browsable(false)]
        public string Value
        {
            get { return textbox.Text; }
            set { textbox.Text = value; }
        }


        public override void SaveSettings(ref object stateObject)
        {            
            //stateObject = Settings;
            object[] lstObj = new object[2];
            lstObj[0] = Settings;
            lstObj[1] = ClientEvents;
            stateObject = lstObj;
            GenericSettingsObject allSettings = new GenericSettingsObject();
            PageHelper.AppendJQueryScript(ClientEvents.GetDefaultEventsScript(), this.Page.Header);


            string targetId = textbox.ClientID;
            string wrapperId = wrapper.Attributes["id"];

            if (Settings.InlineMode)
            {
                targetId = div.Attributes["id"];
                if (!string.IsNullOrEmpty(ClientEvents.OnSelect.Name)) //special case : change event is internally used
                {
                    ClientEvents.OnSelect.script = ClientEvents.OnSelect.Name + "(dateText, inst);";
                    ClientEvents.OnSelect.Name = "";
                }
                ClientEvents.OnSelect.script = "$('#" + textbox.ClientID + "').val(dateText);" + ClientEvents.OnSelect.script;
            }
            if (!string.IsNullOrEmpty(textbox.Text))
                Settings.DefaultDate = "{$.datepicker.parseDate('" + Settings.DateFormat + "', '" + textbox.Text + "')}";
            allSettings.Append(Settings);
            allSettings.Append(ClientEvents);

            

            //if (Settings.InlineMode)
            //{
            //    targetId = div.Attributes["id"];
            //    //GenericSettingsObject allSettings = new GenericSettingsObject();
            //    allSettings.Settings["onSelect"] = new CustomSetting("function(dateText, inst) { $('#" + textbox.ClientID + "').val(dateText) }");                
            //    allSettings.Append(Settings);
            //}
            //if (!string.IsNullOrEmpty(textbox.Text))
            //    Settings.DefaultDate = "{$.datepicker.parseDate('" + Settings.DateFormat + "', '" + textbox.Text + "')}";

            PageHelper.AppendJQueryPlugin("#" + targetId, "datepicker", allSettings.ToString(), this.Page.Header);
        }
        public override void LoadSettings(object stateObject)
        {
            //Settings = (stateObject as DNJDatePickerSettings);
            object[] lstObj = (object[])stateObject;
            Settings = (lstObj[0] as DNJDatePickerSettings);
            ClientEvents = (lstObj[1] as DNJDatePickerEvents);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (DesignMode)
            {
                if (Settings.InlineMode)
                {
                    Assembly assembly = Assembly.GetExecutingAssembly();

                    Stream stream = assembly.GetManifestResourceStream("Org.Eurekaa.DNJ.UI.Resources.designer.DNJDatePicker.html");
                    StreamReader streamReader = new StreamReader(stream);
                    String designerHtml = streamReader.ReadToEnd();

                    textbox.Style.Add("display", "none");
                    writer.Write(designerHtml);
                }
                else
                {
                    textbox.Style.Remove("display");
                }
            }

            textbox.RenderControl(writer);
            div.RenderControl(writer);
        }
        
        protected override void CreateChildControls()
        {
            //Controls.Clear();

            
            string targetId = textbox.ClientID;
            
            if (Settings.InlineMode)
            {
                targetId = "div_" + this.ClientID;
                div.Attributes.Add("width", "200px");
                div.Attributes.Add("height", "200px");
                div.Attributes.Add("id", targetId);
                textbox.Style.Add("display", "none");

                div.Controls.Add(textbox);
                Controls.Add(div);
            }

            Controls.Add(textbox);
            
        }
    }



}
